/*
 * ProgressBar.cpp
 *
 *  Created on: May 5, 2012
 *      Author: gonzalo
 */

#include "ProgressBar.h"
#include "../../common/utils/SurfaceUtils.h"


#define PROGRESSBAR_FONT_SIZE 6

ProgressBar::ProgressBar(int porcent, int posX, int posY, Widget* parent) :
	Widget(Color::ROJO, PROGRESS_BARRA_WIDTH, PROGRESS_BARRA_HEIGHT, posX,posY, parent) {
	this->porcent = porcent;

	int width = porcent * PROGRESS_BARRA_WIDTH / 100;

	this->fillSurface = SurfaceUtils::colorSurface(width,PROGRESS_BARRA_HEIGHT, Color::VERDE);
}

void ProgressBar::showOn(SDL_Surface* surface) {

	SDL_Rect fillRect = this->parcel->getRectangle();
	fillRect.w = fillSurface->w;
	fillRect.h = fillSurface->h;

	Widget::showOn(surface);

	SDL_BlitSurface(fillSurface, NULL, surface, &fillRect);

}

int ProgressBar::getPorcent() {
	return this->porcent;
}

void ProgressBar::updatePorcent() {
	int width = porcent * PROGRESS_BARRA_WIDTH / 100;

	SDL_FreeSurface(fillSurface);
	fillSurface = SurfaceUtils::colorSurface(width,PROGRESS_BARRA_HEIGHT, Color::VERDE);
}

int ProgressBar::reducePercent(int value){
	int newPorcent = porcent - value;
	int result = 0;
	if(newPorcent >= 0){
		porcent = newPorcent;
	}else{
		result = fabs(newPorcent);
		porcent = 0;
	}

	//Actualizo el porcentaje de vida que queda
	updatePorcent();

	//Devuelve la diferencia del valor en caso de que no haya sido absorbido enteramente
	return result;
}

void ProgressBar::resetBar(int porcent){
	this->porcent = porcent;

	int width = porcent * PROGRESS_BARRA_WIDTH / 100;
	SDL_FreeSurface(fillSurface);
	fillSurface = SurfaceUtils::colorSurface(width,PROGRESS_BARRA_HEIGHT, Color::VERDE);
}

ProgressBar::~ProgressBar() {
	SDL_FreeSurface(fillSurface);
}

